<?php
include("../fpdf/fpdf.php");
include("../db_connect.php");

class PDF extends FPDF
{
//include 
//setting the statements in loaddata()


	//$m_logo = "";
	//$m_logo = '../../images/mlogo.png';
	
	// Page header
	function Header()
	{
		// Logo ('location',x,y,w,h) ;
		$this->Image('../images/logo.png',140,5,80,20);
		// Arial bold 15
		$this->SetFont('Arial','B',15);
		// Move to the right
		$this->Cell(1);
		//set the text color
		
		// Title cell(x,y,title,border,?,?);
		$this->Cell(340,30,'Calumpit, Bulacan',0,0,'C');
		// Line break
		$this->Ln(10);	
		
		$this->SetFont('Arial','I',15);
		$this->Cell(150,25,'Basci Auto Sales Payment Report and Delivery',0,0,'C');
		$this->Ln(20);	
		
		
	}

	// Page footer
	function Footer()
	{
		// Position at 1.5 cm from bottom
		$this->SetY(-15);
		// Arial italic 8
		$this->SetFont('Arial','',8);
		// Page number
		$this->Cell(0,10,'Page '.$this->PageNo().' Bascipayment List',0,0,'C');
		
	}
	
	function LoadData(){
		include("../db_connect.php");	
		
		$pdate = "";
		$replace[] = ' ';
		$find[] = ',';
		$fname 	="";
		$lname 	="";
		$email  ="";
		$ptype 	="";
		$txn_id ="";
		$delivered ="";
		
		if(isset($_GET['fname'])){
		$fname = mysql_real_escape_string(htmlentities(trim( $_GET['fname'])));
		$fname = str_replace($find, $replace, $fname);
		}
		if(isset($_GET['lname'])){
		$lname = mysql_real_escape_string(htmlentities(trim( $_GET['lname'])));
		$lname = str_replace($find, $replace, $lname);
		}
		if(isset($_GET['email'])){
		$email = mysql_real_escape_string(htmlentities(trim( $_GET['email'])));
		$email = str_replace($find, $replace, $email);
		}
		if(isset($_GET['ptype'])){
		$ptype = mysql_real_escape_string(htmlentities(trim( $_GET['ptype'])));
		$ptype = str_replace($find, $replace, $ptype);
		}
		if(isset($_GET['txn_id'])){
		$txn_id = mysql_real_escape_string(htmlentities(trim( $_GET['txn_id'])));
		$txn_id = str_replace($find, $replace, $txn_id);
		}
		if(isset($_GET['delivered'])){
		$delivered = mysql_real_escape_string(htmlentities(trim( $_GET['delivered'])));
		$delivered = str_replace($find, $replace, $delivered);
		}

		$sql = "SELECT * FROM tblpayment";
		$sql .= " WHERE fname like '".$fname."%' "; 

		if( $lname !="" ){
		$sql .= " AND lname like '".$lname."%'";
		}
		if($email !="" ){
		$sql .= " AND email like '".$email."%'";
		}
		if($ptype !="" ){
		$sql .= " AND lname like '".$lname."%'";
		}
		if($txn_id !="" ){
		$sql .= " AND txn_id like '".$txn_id."%'";
		}
		if($delivered !="" ){
		$sql .= " AND delivered like '".$delivered."%'";
		}

		$sql .=" ORDER BY transaction_id DESC";


		$result = mysql_query($sql)or die(mysql_error());
		
		while($row=mysql_fetch_row($result)){ 
			$data[] = $row;
		}
		return $data;
		
		
	}


	//Colored table
	// Colored table
	function FancyTable($header, $data){
		
	//Colors, line width and bold font
			//Colors, line width and bold font
			
			$this->Cell(30);
			
			
			$this->SetFillColor(70,70,70);
			$this->SetTextColor(255);
			$this->SetDrawColor(128,0,0);
			$this->SetLineWidth(.3);
			$this->SetFont('Arial','B',8);
			
			//Header
			$w=array(34,15,10,20,60,70,60,20);
			$type="";
			for($i=0;$i<count($header);$i++)
			$this->Cell($w[$i],4,$header[$i],1,0,'C',true);
			
			$this->Ln();
			
			
			//Color and font restoration
			
			$this->SetFillColor(225,235,255);
			$this->SetTextColor(0);
			$this->SetFont('Arial','B',8);
			
			//Data
			$fill=false;
			foreach($data as $row)
			{
				//$header=array('Txn id','Item Code','Qty','Total Pay','Name','Email','Date');
				
				$this->Cell(30);
				
				$name = $this->join($row[9],$row[10]);
				$this->Cell($w[0],5,$row[1],'LR',0,'L',$fill);
				$this->Cell($w[1],5,$row[3],'LR',0,'L',$fill);
				$this->Cell($w[2],5,$row[5],'LR',0,'L',$fill);
				$this->Cell($w[3],5,$row[8],'LR',0,'L',$fill);
				$this->Cell($w[4],5,$name,'LR',0,'L',$fill);
				$this->Cell($w[5],5,$row[11],'LR',0,'L',$fill);
				$this->Cell($w[6],5,$row[12],'LR',0,'L',$fill);
				$this->Cell($w[7],5,$row[13],'LR',0,'L',$fill);
						
				$this->Ln();

				$fill=!$fill;
		
				
			}
			$this->Cell(30);
			$this->Cell(array_sum($w),0,'','T');
			$this->Ln();

			}
			
		
		
	
		
		
	
	function join($fname,$lname){
		
	return $fname . ' ' . $lname;
	}
	

}


$pdf=new PDF('L','mm','Legal');


//Column titles
$header=array('Txn id','Item Code','Qty','Total Pay','Name','Email','Date of Purchase','Delivered');
//Data loading
$data=$pdf->LoadData();

$pdf->SetFont('Arial','',10);
// Move to the right
//$pdf->SetMargins(0.5, 0.5);
$pdf->Cell(1);
$pdf->SetFont('Arial','B',10);
$pdf->Cell(30);
$pdf->Cell('30','5','Basci Auto Sales Payment Report',0,0,'L');
$pdf->Ln();
$pdf->AddPage();
$pdf->FancyTable($header,$data);
$pdf->Output();

mysql_close($con);
?>